package com.sticktogether.multiphotoselect;

import java.util.LinkedList;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sticktogether.R;
import com.sticktogether.server.ServerCommunication;

/**
 * This activity shows a list of all persons of the current session
 * 
 * @author Fabian Stark
 * @since 08.10.13
 */
public class ShowAlbumsActivity extends ListActivity {

    //DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
    private ArrayAdapter<String> adapter;
	private LinkedList<String> 	mListOptions;
	private ListView 			memberList;
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        // remove title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // set FullScreen
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
        	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        else
        	fullScreen();

		
        setContentView(R.layout.activity_session);
        
        memberList = (ListView) findViewById(android.R.id.list);
                
        memberList.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if(position > 0) {
					ServerCommunication.albumid = ServerCommunication.albumids.get(position-1);
					ServerCommunication.topicid = 0;
					ServerCommunication.albumname = (String)((TextView)arg1).getText();
					finish();
				}
				else {
					final EditText input = new EditText(ShowAlbumsActivity.this);
					input.setLines(1);
					new AlertDialog.Builder(ShowAlbumsActivity.this)
				    .setTitle(ShowAlbumsActivity.this.getString(R.string.image_create_album))
				    .setView(input)
				    .setPositiveButton(ShowAlbumsActivity.this.getString(R.string.ok), new DialogInterface.OnClickListener() {
				        public void onClick(DialogInterface dialog, int whichButton) {
				            Editable value = input.getText();
				            if(!value.toString().trim().equals("")) {
				            	ServerCommunication.addAlbum(value.toString().trim(), ShowAlbumsActivity.this);
				            	deleteList();
				            }
				            else {
				            	Toast.makeText(ShowAlbumsActivity.this, getString(R.string.image_album_no_title_added), Toast.LENGTH_SHORT).show();
				            }
				            
				        }
				    })
				    .setNegativeButton(ShowAlbumsActivity.this.getString(R.string.cancel), new DialogInterface.OnClickListener() {
				        public void onClick(DialogInterface dialog, int whichButton) {}
				    }).show();
				}
			}
        });
        
    }
    
    @TargetApi(Build.VERSION_CODES.KITKAT)
	private void fullScreen() {
        View mDecorView = getWindow().getDecorView();
		mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }
    
    @Override
    protected void onResume() {
    	buildList();
    	super.onResume();
    	ServerCommunication.getAlbumListFromServer(this);
    }
    
    public void buildList() {
    	mListOptions = ServerCommunication.albumnames;
    	
        //adapter can listen to changes in the list and apply it on the list view
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mListOptions);
        setListAdapter(adapter);
        if(mListOptions.isEmpty()) {
	        new Thread(new Runnable() {
				
				public void run() {
		        	try {
						Thread.sleep(10000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					while(mListOptions.isEmpty()) {
			        	ServerCommunication.getAlbumListFromServer(ShowAlbumsActivity.this);
			        	try {
							Thread.sleep(10000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}).start();
        }
    }
    
    private void deleteList() {
    	mListOptions.clear();
    }

    
}